package com.store.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.store.app.R;
import com.store.app.utils.scan.DataUtil;

import org.json.JSONArray;
import org.json.JSONException;



public class RecordListAdapter extends BaseAdapter{
	private Context context;		//上下文对象引用
	private  JSONArray datarecords = new JSONArray();

	/**
	 * 构造函数
	 * @param context	上下文
	 */
	public RecordListAdapter(Context context,JSONArray datarecords) {
		this.context = context;
		this.datarecords=datarecords;

	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return datarecords.length();
	}


	@Override
	public Object getItem(int position) {
		return position;
	}
	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if(convertView == null)
		{
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.record_list, null);
			viewHolder.money = (TextView) convertView.findViewById(R.id.money);
			viewHolder.time = (TextView) convertView.findViewById(R.id.time);
			viewHolder.odd_numbers = (TextView) convertView.findViewById(R.id.odd_numbers);
			viewHolder.favorable = (TextView) convertView.findViewById(R.id.favorable);
			viewHolder.tayohya = (TextView) convertView.findViewById(R.id.tayohya);
			viewHolder.way = (TextView) convertView.findViewById(R.id.way);
			viewHolder.song_bi = (TextView) convertView.findViewById(R.id.song_bi);
			viewHolder.deduction = (TextView) convertView.findViewById(R.id.deduction);

			convertView.setTag(viewHolder);			//表示给View添加一个格外的数据，
		} else {
			viewHolder = (ViewHolder)convertView.getTag();//通过getTag的方法将数据取出来
		}
		try {
			viewHolder.money.setText("实收: "+datarecords.getJSONObject(position).getString("pay_amount"));
			viewHolder.time.setText("时间: "+datarecords.getJSONObject(position).getString("created_date"));
			viewHolder.odd_numbers.setText("单号: "+datarecords.getJSONObject(position).getString("flow_no"));
			viewHolder.favorable.setText("减免: "+datarecords.getJSONObject(position).getString("reduce_amount"));
			viewHolder.tayohya.setText("返现: "+datarecords.getJSONObject(position).getString("rebate_cash"));
			viewHolder.song_bi.setText("送币: "+datarecords.getJSONObject(position).getString("reward_gold"));
			viewHolder.deduction.setText("抵扣: "+datarecords.getJSONObject(position).getString("discount_amount"));
			viewHolder.way.setText("支付: "+DataUtil.getZFCodeToString(datarecords.getJSONObject(position).getString("payment_way_key")));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



		return convertView;
	}
	/**
	 * 定义一个内部类
	 * 声明相应的控件引用
	 *
	 */
	public class ViewHolder {

		public TextView money;			//金额
		public TextView time,odd_numbers,favorable,tayohya,way,song_bi;
		public TextView deduction;//抵扣



	}


}